﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.ComponentModel;
    using System.Xml;

    [ScriptType("SP.FieldUserValue", ValueObject=true, ServerTypeId="{c956ab54-16bd-4c18-89d2-996f57282a6f}")]
    public sealed class FieldUserValue : ClientValueObject
    {
        private int m_lookupId;
        private string m_lookupValue;

        public static FieldUserValue FromUser(string userName)
        {
            FieldUserValue value2 = new FieldUserValue {
                LookupId = -1
            };
            value2.SetLookupValue(userName);
            return value2;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = false;
            string str = peekedName;
            if (str != null)
            {
                if (!(str == "LookupValue"))
                {
                    if (str == "LookupId")
                    {
                        flag = true;
                        reader.ReadName();
                        this.m_lookupId = reader.ReadInt32();
                    }
                    return flag;
                }
                flag = true;
                reader.ReadName();
                this.m_lookupValue = reader.ReadString();
            }
            return flag;
        }

        internal void SetLookupValue(string value)
        {
            this.m_lookupValue = value;
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public override void WriteToXml(XmlWriter writer, SerializationContext serializationContext)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (serializationContext == null)
            {
                throw new ArgumentNullException("serializationContext");
            }
            writer.WriteStartElement("Property");
            writer.WriteAttributeString("Name", "LookupValue");
            DataConvert.WriteValueToXmlElement(writer, this.LookupValue, serializationContext);
            writer.WriteEndElement();
            writer.WriteStartElement("Property");
            writer.WriteAttributeString("Name", "LookupId");
            DataConvert.WriteValueToXmlElement(writer, this.LookupId, serializationContext);
            writer.WriteEndElement();
        }

        [Remote]
        public int LookupId
        {
            get
            {
                return this.m_lookupId;
            }
            set
            {
                this.m_lookupId = value;
            }
        }

        [Remote]
        public string LookupValue
        {
            get
            {
                return this.m_lookupValue;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public override string TypeId
        {
            get
            {
                return "{c956ab54-16bd-4c18-89d2-996f57282a6f}";
            }
        }
    }
}

